package hlhj.fhp.supreme.fragment

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.activitys.ArticleAty
import hlhj.fhp.supreme.activitys.MyJiePanAty
import hlhj.fhp.supreme.adapters.MyJiePanAdp
import hlhj.fhp.supreme.javabean.ArticleListBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.toast
import hlhj.fhp.supreme.utils.JsonCallBack
import kotlinx.android.synthetic.main.rcylist.view.*

/**
 * Created by Never Fear   on 2018\7\17 0017.
Never More....
 */
class MyJiePanListFgm :Fragment(), SpringView.OnFreshListener {
    companion object {
        fun getInstence(id:String,name:String): MyJiePanListFgm {
            val jiePanListFgm = MyJiePanListFgm()
            val bundle = Bundle()
            bundle.putString("id",id)
            bundle.putString("name",name)
            jiePanListFgm.arguments=bundle
            return jiePanListFgm
        }
    }
    private lateinit var rootView:View
    private val datas=ArrayList<ArticleListBean.DataBean>()
    private lateinit var adp:MyJiePanAdp
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView= inflater?.inflate(R.layout.rcylist,null)!!
        adp= MyJiePanAdp(datas,activity,activity as MyJiePanAty)
        rootView.recyclerview.adapter=adp
        rootView.recyclerview.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)

        rootView.spView.header=DefaultHeader(context)
        rootView.spView.setListener(this)
        disPatchDta()

        adp.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(context, ArticleAty::class.java)
            intent.putExtra("id",datas[position].id)
            startActivity(intent)
        }
        return rootView
    }

    private fun disPatchDta() {
        OkGo.post<ArticleListBean>(Urls.BaseUrl+Urls.JIEPAN_ARTICLE)
                .params("cat_id",arguments.getString("id"))
                .params("type",1)
                .execute(object :JsonCallBack<ArticleListBean>(activity){
                    override fun onSuccess(response: Response<ArticleListBean>?) {
                        val articleListBean = response?.body()
                        if (articleListBean?.code==1){
                            if (articleListBean.data!=null){
                                rootView.spView.onFinishFreshAndLoad()
                                datas.addAll(articleListBean?.data)
                                adp.notifyDataSetChanged()
                            }
                        }else{
                            this.toast(context,articleListBean?.msg!!)
                        }
                    }
                })
    }

    override fun onLoadmore() {

    }

    override fun onRefresh() {
        datas.clear()
        disPatchDta()
    }
}